// PortalBrowser.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "PortalBrowser.h"
#include "WebBrowserAxDialog.h"
#include "dllmain.h"

struct CREATE_BROWSER_PARAMS
{
	HWND hParentWnd;
	int iCallbackMessage;
	
	LPCWSTR pszPortalName;
	LPCWSTR pszLogin;
	LPCWSTR pszPassword;
	
	HANDLE hInitEvent;
};

class CPortalList: public IPortalList
{
		CPortalBrowserSettings::tPORTAL_LIST m_PortalList;
		
		size_t GetCount() const
		{
			return m_PortalList.size();
		}
		
		LPCWSTR GetName(size_t ind) const
		{
			LPCWSTR pszRet = NULL;
			
			if (ind < GetCount())
				pszRet = m_PortalList[ind].strName.c_str();
				
			return pszRet;
		}
		
		LPCWSTR GetHubUrl(size_t ind) const
		{
			LPCWSTR pszRet = NULL;
			
			if (ind < GetCount())
				pszRet = m_PortalList[ind].strAssociatedHubURL.c_str();
				
			return pszRet;
		}
		
		PORTAL_DATA const *GetData(size_t ind) const
		{
			PORTAL_DATA const *pd = NULL;
			
			if (ind < GetCount())
				pd = &m_PortalList[ind].Data;
				
			return pd;
		}
		
		size_t GetSearchCount(size_t ind) const
		{
			size_t res = 0;
			
			if (ind < GetCount())
				res = m_PortalList[ind].Search.size();
				
			return res;
		}
		
		LPCWSTR GetSearchName(size_t ind, size_t indSearch) const
		{
			LPCWSTR pRet = NULL;
			
			if (ind < GetCount() && indSearch < m_PortalList[ind].Search.size())
				pRet = m_PortalList[ind].Search[indSearch].strName.c_str();
				
			return pRet;
		}
		
		SEARCH_DATA_STRUCT const *GetSearchData(size_t ind, size_t indSearch) const
		{
			SEARCH_DATA_STRUCT const *pRet = NULL;
			
			if (ind < GetCount() && indSearch < m_PortalList[ind].Search.size())
				pRet = &m_PortalList[ind].Search[indSearch].Data;
				
			return pRet;
		}
		
		HBITMAP GetBitmap(size_t ind, bool bSmall) const
		{
			HBITMAP hBMP = NULL;
			if (ind < GetCount())
				hBMP = bSmall ? m_PortalList[ind].hSmallBMP : m_PortalList[ind].hLargeBMP;
				
			return hBMP;
		}
		
	public:
		CPortalList(CPortalBrowserSettings::tPORTAL_LIST &PortalList):
			m_PortalList(PortalList)
		{
		
		}
};

extern "C"
{
	__declspec(dllexport) HANDLE __cdecl CreateBrowser(HWND hParentWnd, int iCallbackMessage, LPARAM lCallbackParam, LPCWSTR pszPortalName, LPCWSTR pszLogin, LPCWSTR pszPassword)
	{
		CWebBrowserAxDialog *pDialog = new CWebBrowserAxDialog(hParentWnd, iCallbackMessage, lCallbackParam, pszPortalName, pszLogin, pszPassword);
		
		pDialog->Create(hParentWnd);
		return pDialog;
	}
	
	__declspec(dllexport) bool __cdecl DestroyBrowser(HANDLE hBrowser)
	{
		bool bRet = false;
		
		if (hBrowser)
		{
			CWebBrowserAxDialog *pDialog = reinterpret_cast<CWebBrowserAxDialog *>(hBrowser);
			//pDialog->DestroyWindow();
			delete pDialog;
			bRet = true;
		}
		
		return bRet;
	}
	
	__declspec(dllexport) HWND __cdecl GetBrowserWnd(HANDLE hBrowser)
	{
		HWND hWnd = NULL;
		
		if (hBrowser)
		{
			CWebBrowserAxDialog *pDialog = reinterpret_cast<CWebBrowserAxDialog *>(hBrowser);
			hWnd = *pDialog;
		}
		
		return hWnd;
	}
	
	__declspec(dllexport) BOOL __cdecl TranslateBrowserAccelerator(HANDLE hBrowser, MSG *pMsg)
	{
		BOOL bRet = FALSE;
		
		if (hBrowser)
		{
			CWebBrowserAxDialog *pDialog = reinterpret_cast<CWebBrowserAxDialog *>(hBrowser);
			bRet = pDialog->TranslateAccelerator(pMsg);
		}
		
		return bRet;
	}
	
	__declspec(dllexport) IPortalList const *__cdecl GetPortalList()
	{
		CPortalBrowserSettings::tPORTAL_LIST list = CPortalBrowserSettings::GetPortalsList();
		return new CPortalList(list);
	}
	
	__declspec(dllexport) void __cdecl FreePortalList(IPortalList const *pPortalList)
	{
		if (pPortalList)
			delete static_cast<CPortalList *>(const_cast<IPortalList*>(pPortalList));
	}
} // extern "C"